/*****************************************************************
/
/     filename:   loggerlib.c
/
/  description:   Implements a logging utility
/
/       author:   Szechy, Jared
/     login id:   cps444-n1.10
/
/        class:   CPS 444
/   instructor:   Perugini
/   assignment:   Homework #3
/
/     assigned:   August 23, 2009
/          due:   October 2, 2009
/
/***************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "loggerlib.h"

typedef struct list_struct
{
   data_t item;
   struct list_struct *next;
} log_t;

static log_t *headptr = NULL;
static log_t *tailptr = NULL;

int addmsg(data_t data)
{
   log_t *newNode = malloc(sizeof(log_t));
   if (newNode == NULL)
   {
      errno = ENOMEM;
      perror("Could not allocate memory");
      return -1;
   }
   newNode->item = data;
   newNode->next = NULL;

   if (headptr == NULL && tailptr == NULL)
   { 
      headptr = newNode;
      tailptr = newNode;
   }
   else
   {
      tailptr->next = newNode;
      tailptr = newNode;
   }

   return 0;
}

void clearlog(void)
{
   if (headptr != NULL)
   {
      log_t *tempPtr = headptr;
      while (headptr != NULL)
      {
         tempPtr = headptr->next;
         free(headptr);
         headptr = tempPtr;
      }
      headptr = NULL;
      tailptr = NULL;
   }     
}

char *getlog(void)
{
   char *logString = malloc(sizeof (char)*19);
   if (logString == NULL)
   {
      errno = ENOMEM;
      perror("Could no allocate memory");
      return NULL;
   }
   data_t tempItem;
   log_t *tempPtr = headptr;
   char* time = malloc (sizeof (char)*19);
   if (time == NULL)
   {
      errno = ENOMEM;
      perror("Could not allocate memory");
      return NULL;
   }
   strcpy(logString,"");

   if (tempPtr != NULL)
   {
      while (tempPtr != NULL)
      {
         tempItem = tempPtr->item;
         struct tm* loct = localtime(&tempItem.time);
         strftime (time, 18, "%x %X ", loct);
         logString = (char *)realloc(logString, strlen(logString) + strlen(time) + strlen(tempItem.string) + 3);
         if (logString == NULL)
         {
            errno = ENOMEM;
            perror("Could not allocate memory");
            return NULL;
         }
         strcat(logString,time);
         strcat(logString," ");
         strcat(logString,tempItem.string);
         strcat(logString,"\n");
         tempPtr = tempPtr->next;
      }
      return logString;
   }
   else
      return NULL;
}

int savelog(char *filename)
{
   if (headptr != NULL)
   {
      FILE * logFile = NULL;
      logFile = fopen(filename,"w");
      if (logFile == NULL)
      {
         errno = EPERM;
         perror("Could not open output file");
         return -1;
      }
      data_t tempItem;
      log_t *tempPtr = headptr;
      char* s = malloc (sizeof (char)*19);
    

      while (tempPtr != NULL)
      {
         tempItem = tempPtr->item;
         struct tm* loct = localtime(&tempItem.time);
         strftime (s, 18, "%x %X ", loct);
         fprintf (logFile, "%s %s\n", s, tempItem.string);
         tempPtr = tempPtr->next;
      }
      fclose(logFile);
   }
   return 0;
}
